package com.hxzy.research.controller.admin;

import com.hxzy.research.pojo.TblUser;
import com.hxzy.research.service.pageandmanager.AdminUserLogin;
import com.hxzy.research.vo.AdminResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/admin")
public class UserLoginController {
    @Autowired private AdminUserLogin adminUserLogin;
    @RequestMapping("/userLogin.go")
    public @ResponseBody
    AdminResultVo userLogin(TblUser tblUser, HttpSession session)throws Exception{
        //System.out.println("*******************************************"+tblUser.getUsername()+tblUser.getPassword());
        //接受查询到的用户信息
        List<TblUser> userList=adminUserLogin.findUserList();
        //遍历判断用户名密码是否正确
        AdminResultVo adminResultVo=null;
        int a=0;
        for (TblUser user:userList){
            if (user.getUsername().equals(tblUser.getUsername())&&user.getPassword().equals(tblUser.getPassword())){
                //adminResultVo.setState("OK");
                adminResultVo=new AdminResultVo("OK");
                session.setAttribute("username",user.getUsername());
                a++;

            }
        }
        if (a==0){
            adminResultVo=new AdminResultVo("用户名或密码错误");
        }
        return adminResultVo;
    }
}
